package org.dev.lms.controller;

import org.dev.auth.module.entity.SysUser;
import org.dev.auth.module.service.SysUserService;
import org.dev.common.security.jwt.JwtUtil;
import org.dev.lms.entity.Book;
import org.dev.lms.service.BookBorrowService;
import org.dev.lms.entity.BookBorrow;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.dev.common.core.page.PaginAtion;
import org.dev.common.core.result.ResponseResult;
import org.dev.lms.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.dev.common.core.aop.OperLog;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/bookBorrow")
public class BookBorrowController {
    @Autowired
    public BookBorrowService bookBorrowService;
    @Autowired
    BookService bookService;
    @Autowired
    SysUserService sysUserService;

    /**
     * 保存和修改公用的
     *
     * @param bookBorrow 传递的实体
     * @return ResponseResult转换结果
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @OperLog(description = "bookBorrow保存和修改")
    public ResponseResult<String> save(@RequestBody BookBorrow bookBorrow) {
        boolean result = false;
        try {
            if (bookBorrow.getId() != null) {
                result = bookBorrowService.updateById(bookBorrow);
            } else {
                bookBorrow.setAdminId(JwtUtil.CurrentUserId());
                bookBorrow.setStatus("0");
                result = bookBorrowService.save(bookBorrow);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        }
        if (result)
            return ResponseResult.success();
        return ResponseResult.error();
    }

    /**
     * 保存和修改公用的
     *
     * @param bookBorrow 传递的实体
     * @return ResponseResult转换结果
     */
    @RequestMapping(value = "/delay", method = RequestMethod.POST)
    @OperLog(description = "bookBorrow保存和修改")
    public ResponseResult<String> delay(@RequestBody BookBorrow bookBorrow) {
        boolean result = false;
        try {
            //增加十五天
            bookBorrow.setEndDate(bookBorrow.getEndDate().plusDays(15));
            result = bookBorrowService.updateById(bookBorrow);
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        }
        if (result)
            return ResponseResult.success();
        return ResponseResult.error();
    }

    /**
     * 获取对象信息
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    @OperLog(description = "bookBorrow获取对象信息")
    public ResponseResult<BookBorrow> get(@RequestParam("id") Long id) {
        BookBorrow bookBorrow = bookBorrowService.getById(id);
        return ResponseResult.success(bookBorrow);
    }

    /**
     * 删除对象信息
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    @OperLog(description = "bookBorrow删除对象信息")
    public ResponseResult<String> delete(@RequestBody List<Long> ids) {
        try {
            bookBorrowService.removeByIds(ids);
            return ResponseResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error("删除对象失败！" + e.getMessage());
        }
    }

    /**
     * 分页查询数据信息
     *
     * @param bookBorrow
     * @param pagination
     * @return
     */
    @RequestMapping("/list")
    @OperLog(description = "bookBorrow分页查询数据信息")
    public ResponseResult<IPage<BookBorrow>> list(BookBorrow bookBorrow, PaginAtion pagination) {
        Page page = pagination.getPage();
        IPage<BookBorrow> bookBorrowIPage = bookBorrowService.page(page, new QueryWrapper<BookBorrow>(bookBorrow));
        List<BookBorrow> bookBorrowList = bookBorrowIPage.getRecords();
        if (bookBorrowList != null && bookBorrowList.size() > 0) {
            List<Long> bookIdArray = bookBorrowList.stream().map(BookBorrow::getBookId).collect(Collectors.toList());
            QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id", bookIdArray);
            List<Book> bookList = this.bookService.list(queryWrapper);
            if (bookList != null && bookList.size() > 0) {
                bookBorrowList.stream().forEach(item -> {
                    bookList.forEach(book -> {
                        if (item.getBookId().equals(book.getId())) {
                            item.setAuthor(book.getAuthor());
                            item.setPublisher(book.getPublisher());
                        }
                    });
                });
            }
            //
            List<Long> adminIds = bookBorrowList.stream().map(BookBorrow::getAdminId).collect(Collectors.toList());
            SysUser sysUser = new SysUser();
            QueryWrapper<SysUser> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.in("id", adminIds);
            List<SysUser> sysUsers = this.sysUserService.list(queryWrapper1);
            if (sysUsers != null && sysUsers.size() > 0) {
                bookBorrowList.stream().forEach(item -> {
                    sysUsers.forEach(user -> {
                        if (item.getAdminId().equals(user.getId())) {
                            item.setAdminName(user.getUsername());
                        }
                    });
                });
            }
        }
        return ResponseResult.success(bookBorrowIPage);
    }

    @RequestMapping("/my-list")
    @OperLog(description = "bookBorrow分页查询数据信息")
    public ResponseResult<IPage<BookBorrow>> myList(BookBorrow bookBorrow, PaginAtion pagination) {
        Page page = pagination.getPage();
        bookBorrow.setReaderId(JwtUtil.CurrentUser().getEmployeId());
        IPage<BookBorrow> bookBorrowIPage = bookBorrowService.page(page, new QueryWrapper<BookBorrow>(bookBorrow));
        List<BookBorrow> bookBorrowList = bookBorrowIPage.getRecords();
        if (bookBorrowList != null && bookBorrowList.size() > 0) {
            List<Long> bookIdArray = bookBorrowList.stream().map(BookBorrow::getBookId).collect(Collectors.toList());
            QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id", bookIdArray);
            List<Book> bookList = this.bookService.list(queryWrapper);
            if (bookList != null && bookList.size() > 0) {
                bookBorrowList.stream().forEach(item -> {
                    bookList.forEach(book -> {
                        if (item.getBookId().equals(book.getId())) {
                            item.setAuthor(book.getAuthor());
                            item.setPublisher(book.getPublisher());
                        }
                    });
                });
            }
            //
            List<Long> adminIds = bookBorrowList.stream().map(BookBorrow::getAdminId).collect(Collectors.toList());
            SysUser sysUser = new SysUser();
            QueryWrapper<SysUser> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.in("id", adminIds);
            List<SysUser> sysUsers = this.sysUserService.list(queryWrapper1);
            if (sysUsers != null && sysUsers.size() > 0) {
                bookBorrowList.stream().forEach(item -> {
                    sysUsers.forEach(user -> {
                        if (item.getAdminId().equals(user.getId())) {
                            item.setAdminName(user.getUsername());
                        }
                    });
                });
            }
        }
        return ResponseResult.success(bookBorrowIPage);
    }

}